package net.codesree.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
  
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class TokenUtil {

 public TokenUtil() {}
 
 public String generateToken() {
  try {
   byte now[] = new Long(System.currentTimeMillis()).toString().getBytes();
   MessageDigest md = MessageDigest.getInstance("MD5");
   md.update(now);
   return toHex(md.digest());
  } catch (IllegalStateException e) {
   return null;
  } catch (NoSuchAlgorithmException e) {
   return null;
  }

 }
 
 public String toHex(byte buffer[]) {
  StringBuffer sb = new StringBuffer();
  String s = null;
  for (int i = 0; i < buffer.length; i++) {
   s = Integer.toHexString((int) buffer[i] & 0xff);
   if (s.length() < 2) {
    sb.append('0');
   }
   sb.append(s);
  }
  return sb.toString();
 } 
}